package yevgeniy.melnichuk.selenium;

import java.util.ArrayList;
import java.util.List;

import yevgeniy.melnichuk.selenium.worker.ClickAndWaitSeleniumWorker;
import yevgeniy.melnichuk.selenium.worker.ClickSeleniumWorker;
import yevgeniy.melnichuk.selenium.worker.IsTextPresentSeleniumWorker;
import yevgeniy.melnichuk.selenium.worker.OpenSeleniumWorker;
import yevgeniy.melnichuk.selenium.worker.SeleniumWorker;
import yevgeniy.melnichuk.selenium.worker.SleepSeleniumWorker;
import yevgeniy.melnichuk.selenium.worker.TypeSeleniumWorker;

import com.thoughtworks.selenium.Selenium;

/**
 * enques all the method calls and executes them all, when the <i>run</i> method
 * is called.
 * 
 * @author Yevgeniy Melnichuk
 */
public class AsynchronousSeleniumBuilder {
	private static final int DEFAULT_WAIT_FOR_PAGE = 20;

	private Selenium selenium;

	List<SeleniumWorker> workers = new ArrayList<SeleniumWorker>();

	public AsynchronousSeleniumBuilder(Selenium selenium) {
		this.selenium = selenium;
	}

	public AsynchronousSeleniumBuilder type(String q, String value) {
		workers.add(new TypeSeleniumWorker(selenium, q, value));
		return this;
	}

	public AsynchronousSeleniumBuilder click(String q) {
		workers.add(new ClickSeleniumWorker(selenium, q));
		return this;
	}

	public AsynchronousSeleniumBuilder clickAndWait(String q) {
		workers.add(new ClickAndWaitSeleniumWorker(selenium, q, DEFAULT_WAIT_FOR_PAGE));
		return this;
	}

	public AsynchronousSeleniumBuilder sleep(int sek) {
		workers.add(new SleepSeleniumWorker(selenium, sek));
		return this;
	}

	public AsynchronousSeleniumBuilder isTextPresent(String text) {
		workers.add(new IsTextPresentSeleniumWorker(selenium, text));
		return this;
	}

	public AsynchronousSeleniumBuilder open(String url) {
		workers.add(new OpenSeleniumWorker(selenium, url));
		return this;
	}

	public void run() {
		for (SeleniumWorker worker : workers) {
			worker.run();
		}
	}
}
